<?php
include_once __DIR__ . '/../BaseShell.php';
//今日时间
$time_data = strtotime(date("Ymd"));
//坐席调解员
$mediator = Module_User_Administrator::getInstance()->getAllBySearch(['id','name','workerId','workerId2','mediationOrganId'],['where'=>['workerId <> 0 or workerId2 <> 0 ']]);
foreach ($mediator as $data){
    if($data['workerId'] > 0) {
        $report = Lib_Redis_Hash::hGetAll($data['workerId']);
        //呼入报表
        $callin_on = empty($report['callin_on']) ? 0 : $report['callin_on'];
        $callin_not = empty($report['callin_not']) ? 0 : $report['callin_not'];
        $callinSave = [
            'callCount'=> $callin_on + $callin_not,
            'callTime'=> empty($report['callin_time']) ? 0 : $report['callin_time'],
            'avgCallTime' => empty($report['callin_time']) ? 0 : ceil($data['callin_time'] / $callin_on),
            'longCallTime'=> empty($report['callin_max_long']) ? 0 : $report['callin_max_long'],
            'receivedNum'=> $callin_on,
            'missedCallsNum'=> $callin_not,
            'callCompletionRate'=> $callin_on ? ceil($callin_on / ($callin_on + $callin_not)) * 100 : 0,
            'workerId'=>$data['workerId'],
        ];
        $history = Module_Mediate_CallinReportforms::getInstance()->getOne([],["countTime = {$time_data}","mediatorId = {$data['id']}","workerId = {$data['workerId']}"]);
        if($history){
            $callinSave['id'] = $history['id'];
            $res = Module_Mediate_CallinReportforms::getInstance()->singleUpdate($callinSave);
        }else{
            $callinSave['mediatorId'] = $data['id'];
            $callinSave['mediatorName'] = $data['name'];
            $callinSave['mediationOrganId'] = $data['mediationOrganId'];
            $callinSave['countTime'] = $time_data;
            $callinSave['addTime'] = time();
            $res = Module_Mediate_CallinReportforms::getInstance()->singleInsert($callinSave);
        }

        //呼出报表
        $callout_on = empty($report['callout_on']) ? 0 : $report['callout_on'];
        $callout_not = empty($report['callout_not']) ? 0 : $report['callout_not'];
        $calloutSave = [
            'callCount'=> $callout_on + $callout_not,
            'callTime'=> empty($report['callout_time']) ? 0 : $report['callout_time'],
            'avgCallTime' => empty($report['callout_time']) ? 0 : ceil($data['callout_time'] / $callout_on),
            'longCallTime'=> empty($report['callout_max_long']) ? 0 : $report['callout_max_long'],
            'receivedNum'=> $callout_on,
            'missedCallsNum'=> $callout_not,
            'callCompletionRate'=> $callout_on ? ceil($callout_on / ($callout_on + $callout_not)) * 100 : 0,
            'workerId'=>$data['workerId'],
        ];

        $history = Module_Mediate_CalloutReportforms::getInstance()->getOne([],["countTime = {$time_data}","mediatorId = {$data['id']}","workerId = {$data['workerId']}"]);
        if($history){
            $calloutSave['id'] = $history['id'];
            $res = Module_Mediate_CalloutReportforms::getInstance()->singleUpdate($calloutSave);

        }else{
            $calloutSave['mediatorId'] = $data['id'];
            $calloutSave['mediatorName'] = $data['name'];
            $calloutSave['mediationOrganId'] = $data['mediationOrganId'];
            $calloutSave['countTime'] = $time_data;
            $calloutSave['addTime'] = time();
            $res = Module_Mediate_CalloutReportforms::getInstance()->singleInsert($calloutSave);
        }
    }elseif ($data['workerId2'] > 0){
        $report = Lib_Redis_Hash::hGetAll($data['workerId2']);
        //呼入报表
        $callin_on = empty($report['callin_on']) ? 0 : $report['callin_on'];
        $callin_not = empty($report['callin_not']) ? 0 : $report['callin_not'];
        $callinSave = [
            'callCount'=> $callin_on + $callin_not,
            'callTime'=> empty($report['callin_time']) ? 0 : $report['callin_time'],
            'avgCallTime' => empty($report['callin_time']) ? 0 : ceil($data['callin_time'] / $callin_on),
            'longCallTime'=> empty($report['callin_max_long']) ? 0 : $report['callin_max_long'],
            'receivedNum'=> $callin_on,
            'missedCallsNum'=> $callin_not,
            'callCompletionRate'=> $callin_on ? ceil($callin_on / ($callin_on + $callin_not)) * 100 : 0,
            'workerId'=>$data['workerId2'],
        ];
        $history = Module_Mediate_CallinReportforms::getInstance()->getOne([],["countTime = {$time_data}","mediatorId = {$data['id']}","workerId = {$data['workerId2']}"]);
        if($history){
            $callinSave['id'] = $history['id'];
            $res = Module_Mediate_CallinReportforms::getInstance()->singleUpdate($callinSave);
        }else{
            $callinSave['mediatorId'] = $data['id'];
            $callinSave['mediatorName'] = $data['name'];
            $callinSave['mediationOrganId'] = $data['mediationOrganId'];
            $callinSave['countTime'] = $time_data;
            $callinSave['addTime'] = time();
            $res = Module_Mediate_CallinReportforms::getInstance()->singleInsert($callinSave);
        }

        //呼出报表
        $callout_on = empty($report['callout_on']) ? 0 : $report['callout_on'];
        $callout_not = empty($report['callout_not']) ? 0 : $report['callout_not'];
        $calloutSave = [
            'callCount'=> $callout_on + $callout_not,
            'callTime'=> empty($report['callout_time']) ? 0 : $report['callout_time'],
            'avgCallTime' => empty($report['callout_time']) ? 0 : ceil($data['callout_time'] / $callout_on),
            'longCallTime'=> empty($report['callout_max_long']) ? 0 : $report['callout_max_long'],
            'receivedNum'=> $callout_on,
            'missedCallsNum'=> $callout_not,
            'callCompletionRate'=> $callout_on ? ceil($callout_on / ($callout_on + $callout_not)) * 100 : 0,
            'workerId'=>$data['workerId2'],
        ];

        $history = Module_Mediate_CalloutReportforms::getInstance()->getOne([],["countTime = {$time_data}","mediatorId = {$data['id']}","workerId = {$data['workerId2']}"]);
        if($history){
            $calloutSave['id'] = $history['id'];
            $res = Module_Mediate_CalloutReportforms::getInstance()->singleUpdate($calloutSave);

        }else{
            $calloutSave['mediatorId'] = $data['id'];
            $calloutSave['mediatorName'] = $data['name'];
            $calloutSave['mediationOrganId'] = $data['mediationOrganId'];
            $calloutSave['countTime'] = $time_data;
            $calloutSave['addTime'] = time();
            $res = Module_Mediate_CalloutReportforms::getInstance()->singleInsert($calloutSave);
        }
    }

}

